import requests
from lxml import etree
import csv

start = 0
headers = {
	"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36"
}
datas = []

while True:
	print(f"正在爬取第{start}.......")
	res = requests.get(f"https://movie.douban.com/top250?start={start}&filter=", headers=headers)
	tree = etree.HTML(res.text)
	items = tree.xpath('//ol[@class="grid_view"]/li/div[@class="item"]')
	if not items:
		print(f"爬取结束")
		break

	for item in items:
		title = item.xpath('.//div[@class="hd"]//span[@class="title"]/text()')[0]
		# datas.append({
		# 	   "id": 1 if not datas else datas[-1]["id"] + 1,
		# 		"title": title
		#
		# })
		datas.append([1 if not datas else datas[-1][0] + 1, title])
	# start += 25


print(datas)

with open(f"top250.csv", "w", newline="") as f:
	csv_writer = csv.writer(f)
	csv_writer.writerow(["ID", "Title"])
	csv_writer.writerows(datas)
